package com.smartmore.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.util.Log;
import android.widget.ImageView.ScaleType;

public class RectImageViewFactory {


	public RectImageViewFactory() {		
	}
	
	public static  ArrayList<RectImageView> createRectImageViewList(Context context, int type, String[] imageFilePaths) {
		ArrayList<RectImageView> rectImageViewList = new ArrayList<RectImageView>();
		try {
		int imageLength = imageFilePaths.length;
			if(type == 1 && imageLength == 2) {
				
				RectImageView rectImageView = new RectImageView(context);
				Rect rect = new Rect(0, 0, 200, 480);
				rectImageView.setmImageSizeRect(rect);	
				Bitmap bitmap = getConversionBitmap(context,rectImageView,imageFilePaths[0]);
				rectImageView.setImageBitmap(bitmap);
				rectImageView.setScaleType(ScaleType.MATRIX);
				rectImageViewList.add(rectImageView);

				
				rectImageView = new RectImageView(context);
				rect = new Rect(200, 0, 480, 480);
				rectImageView.setmImageSizeRect(rect);
				bitmap = getConversionBitmap(context,rectImageView,imageFilePaths[1]);
				rectImageView.setImageBitmap(bitmap);
				rectImageView.setScaleType(ScaleType.MATRIX);
				rectImageViewList.add(rectImageView);			
			}
			
			if(type == 2 && imageLength == 2) {
				RectImageView rectImageView = new RectImageView(context);
				Rect rect = new Rect(0, 0, 480, 240);
				rectImageView.setmImageSizeRect(rect);	
				Bitmap bitmap = getConversionBitmap(context,rectImageView,imageFilePaths[0]);
				rectImageView.setImageBitmap(bitmap);
				rectImageView.setScaleType(ScaleType.MATRIX);
				rectImageViewList.add(rectImageView);

				
				rectImageView = new RectImageView(context);
				rect = new Rect(0, 240, 480, 480);
				rectImageView.setmImageSizeRect(rect);
				bitmap = getConversionBitmap(context,rectImageView,imageFilePaths[1]);
				rectImageView.setImageBitmap(bitmap);
				rectImageView.setScaleType(ScaleType.MATRIX);
				rectImageViewList.add(rectImageView);			
			}
			
			if(type == 1 && imageLength == 4) {
				RectImageView rectImageView = new RectImageView(context);
				Rect rect = new Rect(0, 0, 240, 240);
				rectImageView.setmImageSizeRect(rect);	
				Bitmap bitmap = getConversionBitmap(context,rectImageView,imageFilePaths[0]);
				rectImageView.setImageBitmap(bitmap);
				rectImageView.setScaleType(ScaleType.MATRIX);
				rectImageViewList.add(rectImageView);

				
				rectImageView = new RectImageView(context);
				rect = new Rect(240, 0, 480, 240);
				rectImageView.setmImageSizeRect(rect);
				bitmap = getConversionBitmap(context,rectImageView,imageFilePaths[1]);
				rectImageView.setImageBitmap(bitmap);
				rectImageView.setScaleType(ScaleType.MATRIX);
				rectImageViewList.add(rectImageView);
				
				rectImageView = new RectImageView(context);
				rect = new Rect(0, 240, 240, 480);
				rectImageView.setmImageSizeRect(rect);
				bitmap = getConversionBitmap(context,rectImageView,imageFilePaths[2]);
				rectImageView.setImageBitmap(bitmap);
				rectImageView.setScaleType(ScaleType.MATRIX);
				rectImageViewList.add(rectImageView);
				
				rectImageView = new RectImageView(context);
				rect = new Rect(240, 240, 480, 480);
				rectImageView.setmImageSizeRect(rect);
				bitmap = getConversionBitmap(context,rectImageView,imageFilePaths[3]);
				rectImageView.setImageBitmap(bitmap);
				rectImageView.setScaleType(ScaleType.MATRIX);
				rectImageViewList.add(rectImageView);							
				
			}
			
			
		}catch(Exception ex) {
			Log.e("error",ex.getMessage(),ex);
		}
		
		return rectImageViewList;
	}
	
	public static Bitmap getConversionBitmap(Context context, RectImageView rectImageView, String imageFilePath) throws Exception {
		
		Rect rect = rectImageView.getmImageSizeRect();
		
		BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
		bmpFactoryOptions.inJustDecodeBounds = true;
		int orgImageWidth = 0;
		int orgImageHeight = 0;
		
		int degree = getRotationDegree(imageFilePath);
		//InputStream  in = new FileInputStream(new File(imageFilePath)); 
		BitmapFactory.decodeStream( new FileInputStream(new File(imageFilePath)), null, bmpFactoryOptions);
		orgImageWidth = bmpFactoryOptions.outWidth;
		orgImageHeight = bmpFactoryOptions.outHeight;
			    
		int sampleSize = 1;
		if(rect.width() > rect.height()) {
			if(degree == 90 || degree == 270) {
				sampleSize = (int)orgImageHeight/rect.width();
			}else {
				sampleSize = (int)orgImageWidth/rect.width();
			}
		}else {
			if(degree == 90 || degree == 270) {
				sampleSize = (int)orgImageWidth/rect.height();
			}else {
				sampleSize = (int)orgImageHeight/rect.height();
			}
		}
		if(sampleSize < 1) {
			sampleSize = 1;
		}
		
		bmpFactoryOptions.inSampleSize = sampleSize;
		bmpFactoryOptions.inJustDecodeBounds = false;
		Bitmap returnBmp = BitmapFactory.decodeStream( new FileInputStream(new File(imageFilePath)), null, bmpFactoryOptions);
		Log.d("sampleImage", "before sampleSize=>"+sampleSize+ ","+returnBmp.getWidth()+","+returnBmp.getHeight());
		returnBmp = rotate(returnBmp, degree);	
		Log.d("sampleImage", "middle sampleSize=>"+sampleSize+ ","+returnBmp.getWidth()+","+returnBmp.getHeight());
		
		
		int imageWidth = returnBmp.getWidth();
		int imageHeight = returnBmp.getHeight();
		if(rect.width() > rect.height()) {
			float widthRatio = ((float)rect.width()/(float)imageWidth);			
			Log.d("(int)(imageHeight*widthRatio)",rect.width()+","+imageWidth+","+ widthRatio+","+(int)(imageHeight*widthRatio) );
			returnBmp =Bitmap.createScaledBitmap(returnBmp, rect.width(),(int)(imageHeight*widthRatio) , false);
		}else if(rect.width() == rect.height()){
			if(imageWidth > imageHeight) {
				float heightRatio = ((float)rect.height())/(float)imageHeight;
				returnBmp =Bitmap.createScaledBitmap(returnBmp, (int)(imageWidth*heightRatio),rect.height() , false);							
			}else {
				float heightRatio = ((float)rect.width())/((float)imageWidth);
				returnBmp =Bitmap.createScaledBitmap(returnBmp, rect.width(),(int)(imageHeight*heightRatio) , false);											
			}
		}else {
			float heightRatio = ((float)rect.height())/(float)imageHeight;
			returnBmp =Bitmap.createScaledBitmap(returnBmp, (int)(imageWidth*heightRatio),rect.height() , false);						
		}
		
		Log.d("sampleImage", "after sampleSize=>"+sampleSize+ ","+returnBmp.getWidth()+","+returnBmp.getHeight());
	
					
//		int widthRatio = (int)Math.ceil(orgImageWidth/rect.width());
//		int heightRatio = (int)Math.ceil(orgImageHeight/rect.height());
//				
//		if(widthRatio > 1 && heightRatio > 1) {			
//			if(widthRatio < heightRatio) {				
//				bmpFactoryOptions.inSampleSize = widthRatio;
//			}else {
//				bmpFactoryOptions.inSampleSize = heightRatio;
//			}
//		}
//
//		
//
//		
//		bmpFactoryOptions.inJustDecodeBounds = false;
//		Bitmap returnBmp = BitmapFactory.decodeStream( new FileInputStream(new File(imageFilePath)), null, bmpFactoryOptions);
//		
//		if(degree == 90 || degree == 270) {
//			returnBmp = rotate(returnBmp, degree);		
//		}
//		
//		int width = returnBmp.getWidth();
//		int height = returnBmp.getHeight();
//		if(rect.width() > rect.height()) {
//			if(width > height) {
//				returnBmp =Bitmap.createScaledBitmap(returnBmp, rect.width(),rect.width() , false);
//			}else {
//				returnBmp =Bitmap.createScaledBitmap(returnBmp, rect.width(),rect.width() , false);
//			}
//		}else {
//			if(width > height) {
//				returnBmp =Bitmap.createScaledBitmap(returnBmp, rect.height() ,rect.height() , false);
//			}else {
//				returnBmp =Bitmap.createScaledBitmap(returnBmp, rect.height() ,rect.height() , false);
//			}
//		}
		
		
//		float width = 0;
//		float height = 0;
//		if(rect.width() > rect.height()) {
//			width = rect.width();
//			height = rect.height()*(rect.width()/rect.height());
//			returnBmp =Bitmap.createScaledBitmap(returnBmp, (int)width,(int)height , false);
//			//returnBmp = Bitmap.createScaledBitmap(returnBmp, 480, 640, false);
//		}else {
//			Log.d("width",rect.width()+","+(rect.height()/rect.width()));
//			width = rect.width()*(rect.height()/rect.width());
//			height = rect.height();
//			returnBmp = Bitmap.createScaledBitmap(returnBmp, (int)width, (int)height, false);
//		}
		
		
		
		
		return returnBmp;
		
	}
	
	public static Bitmap rotate(Bitmap image, int degrees)
	{
		if(degrees != 0 && image != null)
		{
			Matrix m = new Matrix();
			m.setRotate(degrees, (float)image.getWidth()/2, (float)image.getHeight()/2);			
			try
			{
				Log.d("rotate","=>"+image.getWidth()+","+image.getHeight());
				Bitmap b2 = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), m, true);	
				
				//b2  = Bitmap.createScaledBitmap(b2,b2.getWidth(), image.getHeight(), true);
				
				if(image != b2)
				{
					image.recycle();
					image = b2;
				}
			} catch(OutOfMemoryError ex)
			{
				// We have no memory to rotate. Return the original bitmap.
			}
		}
		return image;
	}
	
	public synchronized static int getRotationDegree(String filepath) 
	{
	    int degree = 0;
	    ExifInterface exif = null;
	    
	    try 
	    {
	        exif = new ExifInterface(filepath);
	    } 
	    catch (IOException e) 
	    {
	        e.printStackTrace();
	    }
	    
	    if (exif != null) 
	    {
	        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
	        
	        if (orientation != -1) 
	        {
	            // We only recognize a subset of orientation tag values.
	            switch(orientation) 
	            {
	                case ExifInterface.ORIENTATION_ROTATE_90:
	                    degree = 90;
	                    break;
	                    
	                case ExifInterface.ORIENTATION_ROTATE_180:
	                    degree = 180;
	                    break;
	                    
	                case ExifInterface.ORIENTATION_ROTATE_270:
	                    degree = 270;
	                    break;
	            }

	        }
	    }
	    
	    return degree;
	}
}
